package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Model.ConferenceManager;
import Model.User;

/**
 * Panel that allows user to input information for
 * a new conference.  After User inputs conference info
 * they can add the conference to the system.
 * 
 * @author Paul Schultz (ps87@uw.edu)
 * @version 12/01/2012
 */
@SuppressWarnings("serial")
public class AddConferencePanel extends JPanel
{
  private final Font DEFAULT_FONT = new Font("Plain", Font.PLAIN, 16);
  private ConferenceManager my_manager;
  private ConferenceWizardFrame my_frame;
  private JTextField my_conf_name;
  private JTextField my_month;
  private JTextField my_day;
  private JTextField my_year;
  
  /**
   * Constructs AddConferencePanel that allows user to
   * create a new conference.
   * 
   * @param the_manager ConferenceManager to add the conference to.
   * @param the_frame Reference to the master frome of the application.
   */
  public AddConferencePanel(final ConferenceManager the_manager, 
      final ConferenceWizardFrame the_frame)
  {
    super();
    my_manager = the_manager;
    my_frame = the_frame;
    
    this.setLayout(new BorderLayout());
    
    //Set up North area.
    JLabel header = new JLabel("Add New Conference");
    header.setForeground(Color.BLUE);
    header.setFont(new Font("Plain", Font.PLAIN, 38));
    
    JPanel north_panel = new JPanel();
    north_panel.setLayout(new BoxLayout(north_panel, BoxLayout.Y_AXIS));
    north_panel.setBackground(Color.WHITE);
    north_panel.add(header);
    
    //Set up Center area.
    JPanel center_panel = new JPanel();
    center_panel.setLayout(new BoxLayout(center_panel, BoxLayout.Y_AXIS));
    center_panel.setBackground(Color.WHITE);
    
    JPanel cName = new JPanel();
    cName.setMaximumSize(new Dimension(800, 50));
    JPanel pChair = new JPanel();
    pChair.setMaximumSize(new Dimension(800, 50));
    
    JLabel pcInfo = new JLabel("Program Chair: " + my_manager.getCurrentUser().realName());
    pcInfo.setFont(DEFAULT_FONT);
    
    JLabel cLabel = new JLabel("Conference Name:");
    cLabel.setFont(DEFAULT_FONT);
    JPanel cPanel = new JPanel();
    cPanel.add(cLabel);
    
    my_conf_name = new JTextField("Insert Conference Name Here", 30);
    my_conf_name.setFont(DEFAULT_FONT);
    
    JLabel deadline = new JLabel("Paper Submission Deadline:");
    deadline.setFont(DEFAULT_FONT);
    
    my_month = new JTextField("MM");
    my_month.setFont(new Font("Plain", Font.PLAIN, 16));
    my_day = new JTextField("DD");
    my_day.setFont(new Font("Plain", Font.PLAIN, 16));
    my_year = new JTextField("YYYY");
    my_year.setFont(new Font("Plain", Font.PLAIN, 16));
    
    JLabel file = new JLabel("Manuscript File: ");
    file.setFont(DEFAULT_FONT);
  
    JPanel deadlinePanel = new JPanel();
    deadlinePanel.setMaximumSize(new Dimension(800,50));
    
    deadlinePanel.add(deadline);
    deadlinePanel.add(my_month);
    deadlinePanel.add(my_day);
    deadlinePanel.add(my_year);

    pChair.add(pcInfo);
    cName.add(cLabel);
    cName.add(my_conf_name);
    
    center_panel.add(Box.createVerticalStrut(150));
    center_panel.add(pChair);
    center_panel.add(cName);
    center_panel.add(deadlinePanel);
    
    // Set up South area.
    JPanel south_panel = new JPanel();
    south_panel.setBackground(Color.WHITE);
    south_panel.add(Box.createRigidArea(new Dimension(0, 100)));
    
    JButton createConf = new JButton("Create Conference");
    createConf.addActionListener(new CreateConferenceListener());
    //my_submit.setEnabled(false); //Set to false until a paper is uploaded
    
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new CancelAddConferenceListener());
    
    south_panel.add(cancel);
    south_panel.add(createConf);
    
    add(south_panel, BorderLayout.SOUTH);
    
    // Add NORTH, CENTER, and SOUTH to designated areas.
    add(center_panel, BorderLayout.CENTER);
    add(north_panel, BorderLayout.NORTH);
  }
  
  /**
   * Listener used for Add Conference button.
   */
  public class CreateConferenceListener extends AbstractAction
  {
    /**
     * Adds the paper to the conference and then returns GUI
     * to the Author Tool Panel.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      int month = 0;
      int day = 0;
      int year = 0;
      boolean dateValid = true;
      
      //Checks date fields for valid input.
      try
      {
        month = Integer.parseInt(my_month.getText());
        day = Integer.parseInt(my_day.getText());
        year = Integer.parseInt(my_year.getText()) - 1900;
      }
      catch (Exception e)
      {
        dateValid = false;
      }
      
      //Adds conference if date is valid format, else does nothing.
      if (dateValid)
      {
        List<User> users = new ArrayList<User>(my_manager.getUsers());
        my_manager.addConference(my_conf_name.getText(), my_manager.getCurrentUser(), 
            users, new Date(year, month, day));
        my_frame.setCurrentPanel(new ConferenceListPanel(my_manager, my_frame));
      }
    }
  }
  
  /**
   * Listener used for Cancel button.
   */
  public class CancelAddConferenceListener extends AbstractAction
  {
    /**
     * Returns to ConferenceList if Cancel button is pressed.
     */
    public void actionPerformed(ActionEvent e)
    {
      my_frame.setCurrentPanel(new ConferenceListPanel(my_manager, my_frame));
    }
  }
}
